﻿using System.Collections.Generic;
using System.IO;

namespace Engine.FileFormats3D
{
    public class MoxMaterialFile
    {
        #region Private Fields

        private string ColSetInf;
        private List<MoxMaterial> materials;
        private List<string> unusedData;

        #endregion Private Fields

        #region Public Constructors

        public MoxMaterialFile()
        {
            this.materials = new List<MoxMaterial>();
            this.unusedData = new List<string>();
        }

        #endregion Public Constructors

        #region Public Properties

        public List<MoxMaterial> Materials
        {
            get { return materials; }
            set { materials = value; }
        }

        #endregion Public Properties

        #region Public Methods

        public static MoxMaterialFile FromFile(string filename)
        {
            MoxMaterialFile materialFile = new MoxMaterialFile();
            if (File.Exists(filename))
            {
                FileStream fs = new FileStream(filename, FileMode.Open);
                StreamReader sr = new StreamReader(fs);

                materialFile = MoxMaterialFile.FromStream(sr);

                sr.Close();
                fs.Close();
            }
            return materialFile;
        }

        #endregion Public Methods

        #region Private Methods

        private static MoxMaterialFile FromStream(StreamReader sr)
        {
            MoxMaterialFile materialFile = new MoxMaterialFile();
            string line;

            while ((line = sr.ReadLine()) != null)
            {
                string[] lineData = line.Split(' ');

                switch (lineData[0])
                {
                    case "ColSetInf":
                        materialFile.ColSetInf = lineData[1];
                        break;

                    case "#": // new MaterialHeader
                        materialFile.materials.Add(MoxMaterial.FromStream(sr, lineData[1]));
                        break;

                    default:
                        materialFile.unusedData.Add(line);
                        break;
                }
            }

            return materialFile;
        }

        #endregion Private Methods
    }
}